"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : SQL语法
Case Name   : create table as创建列存压缩表,不指定参数compression,默认为low
Create At   : 2023/03/31
Owner       : @zou_jialiang050
Description :
    1.创建源表
    2.create table as创建列存压缩表,不指定参数compression,默认为low
    3.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.Constant = Constant()
        self.table1 = 't_create_table_as_004_01'
        self.table2 = 't_create_table_as_004_02'

    def test_create_table_as(self):
        text = 'text1:创建源表 expect:成功'
        self.logger.info(text)
        create_table = self.primary_sh.execut_db_sql(f'''
            drop table if exists {self.table1};
            create table {self.table1}(id int);''')
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + text)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, create_table,
                      "执行失败" + text)

        text = 'text2:create table as创建列存压缩表,不指定参数compression,' \
               '默认为low expect:成功'
        self.logger.info(text)
        create_table = self.primary_sh.execut_db_sql(
            f'''drop table if exists {self.table2};create table {self.table2} 
            with (orientation=column) as select * from {self.table1}; 
            \\d+ {self.table2}''')
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + text)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, create_table,
                      "执行失败" + text)
        self.assertIn('Options: orientation=column, compression=low',
                      create_table, "执行失败" + text)

    def tearDown(self):
        text = 'text3:清理环境 expect:成功'
        self.logger.info(text)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop table {self.table1} cascade;drop table {self.table2};''')
        self.logger.info(clean_environment)
        self.assertEqual(
            clean_environment.count(self.Constant.TABLE_DROP_SUCCESS), 2,
            "执行失败" + text)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
